<?php

	class Connection {

		private $_errorMessage = '';
		private $_errorNumber = 0;
		private $_affectedRows = 0;
		private $_linkId = 0;
		private $_lastInsertId = 0;
		private $_queryId = 0;
		private static $_instance = null;

		private function __construct() {
			$this->connect( true );
		}

		function __destruct() {
			$this->close();
		}

		public static function getInstance() {
			if( !self::$_instance ) {
				self::$_instance = new self();
			}
			return self::$_instance;
		}

		private function connect( $newLink = false ) {
			$this->_linkId = @mysql_connect( MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, $newLink );
			if( !$this->_linkId ) {
				$this->oops( 'Could not connect to server: <b>' . MYSQL_HOST . '</b>.' );
			}
			if( !@mysql_select_db( MYSQL_DATABASE, $this->_linkId ) ) {
				$this->oops( 'Could not open database: <b>' . MYSQL_DATABASE . '</b>.' );
			}
		}

		private function close() {
			if( !@mysql_close( $this->_linkId ) ) {
				$this->oops( 'Connection close failed!' );
			}
		}

		private function escape( $string ) {
			if( ( get_magic_quotes_runtime() == 1 ) || ( get_magic_quotes_gpc() == 1 ) ) {
				$string = stripslashes( $string );
			}
			return mysql_real_escape_string( $string );
		}

		function query( $sql ) {
			$this->_queryId = @mysql_query( $sql, $this->_linkId );
			if( !$this->_queryId ) {
				$this->oops( "<b>MySQL Query fail:</b> $sql" );
				return 0;
			}
			$this->_affectedRows = @mysql_affected_rows( $this->_linkId );
			return $this->_queryId;
		}

		private function fetchArray( $queryId = -1 ) {
			$record = null;
			if( $queryId > 0 ) {
				$this->_queryId = $queryId;
			}
			if( $this->_queryId > 0 ) {
				$record = @mysql_fetch_assoc( $this->_queryId );
			} else {
				$this->oops( "Invalid query id: <b>$this->_queryId</b>. Records could not be fetched." );
			}
			return $record;
		}

		function FetchAllArray( $sql ) {
			$queryId = $this->query( $sql );
			$out = array();
			while( $row = $this->fetchArray( $queryId ) ) {
				$out[] = $row;
			}
			$this->_freeResult( $queryId );
			return $out;
		}

		private function _freeResult( $queryId = -1 ) {
			if( $queryId > 0 ) {
				$this->_queryId = $queryId;
			}
			if( $this->_queryId > 0 && !@mysql_free_result( $this->_queryId ) ) {
				$this->oops( "Result ID: <b>$this->_queryId</b> could not be freed." );
			}
		}

		function queryFirst( $queryString ) {
			$queryId = $this->query( $queryString );
			$out = $this->fetchArray( $queryId );
			$this->freeResult( $queryId );
			return $out;
		}

		function queryUpdate( $table, $data, $where = '1' ) {
			$q = 'UPDATE `' . $table. '` SET ';
			foreach( $data as $key => $val ) {
				if( strtolower( $val ) == 'null' ) {
					$q .= '`$key` = NULL, ';
				} else if( strtolower( $val ) == 'now()' ) {
					$q .= '`$key` = NOW(), ';
				} else if( strtolower( $val ) == 'unix_timestamp()' ) {
					$q .= '`$key` = UNIX_TIMESTAMP(), ';
				} else {
					$q .= "`$key` = '" . $this->escape( $val ) . "', ";
				}
			}
			$q = rtrim( $q, ', ' ) . ' WHERE ' . $where . ';';
			return $this->query( $q );
		}

		function queryDelete( $table, $where = '1' ) {
			$q = 'DELETE FROM `' . $table . '` WHERE ' . $where . ';';
			return $this->query( $q );
		}

		function queryInsert( $table, $data ) {
			$q = "INSERT INTO `$table` ";
			$v = $n = '';
			foreach( $data as $key => $val ) {
				$n .= "`$key`, ";
				if( strtolower( $val ) == 'null' ) {
					$v .= "NULL, ";
				} else if( strtolower( $val ) == 'now()' ) {
					$v .= "NOW(), ";
				} else if( strtolower( $val ) == 'unix_timestamp()' ) {
					$v .= "UNIX_TIMESTAMP(), ";
				} else {
					$v .= "'" . $this->escape( $val ) . "', ";
				}
			}
			$q .= '(' . rtrim( $n, ', ' ) . ') VALUES (' . rtrim( $v, ', ' ) . ');';
			if( $this->query( $q ) ) {
				$this->_lastInsertId = mysql_insert_id( $this->_linkId );
				return $this->_lastInsertId;
			} else {
				return false;
			}
		}

		function oops( $msg = '' ) {
			if( $this->_linkId > 0 ) {
				$this->_errorMessage = mysql_error( $this->_linkId );
				$this->_errorNumber = mysql_errno( $this->_linkId );
			} else {
				$this->_errorMessage = mysql_error();
				$this->_errorNumber = mysql_errno();
			}
			throw new Exception( 'MySql Error : ' . $this->_errorMessage );
		}
	}